package hn.cch.framework.disruptor;

import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import com.lmax.disruptor.util.DaemonThreadFactory;

public class StringEventDisruptor {

    public static void main(String[] args) throws InterruptedException {
        EventFactory<StringEvent> eventFactory = new StringEventFactory();
        int ringBufferSize = 1024 * 1024; // ringBufferSize=2^n

        Disruptor<StringEvent> disruptor = new Disruptor<>(eventFactory, ringBufferSize,
            DaemonThreadFactory.INSTANCE, ProducerType.SINGLE, new YieldingWaitStrategy());
        disruptor.handleEventsWith(new StringEventHandler()).then(new StringEventConsumer());

        disruptor.start();
        RingBuffer<StringEvent> ringBuffer = disruptor.getRingBuffer();
        StringEventProducer producer = new StringEventProducer(ringBuffer);
        for (int i = 1; i <= 10; i++) {
            producer.onData("data-" + i);
            Thread.sleep(1000);
        }

        Thread.currentThread().join();
        // disruptor.shutdown();
    }
}
